﻿#include "u_userlogin_page.h"

UserLoginPage::UserLoginPage(QWidget *parent) : MyDialog(parent)
{
    this->InitWidget();
    this->SetupConnections();
    this->Initialize();

    this->setTitle("用户登录");
    this->setMaxHide();
    this->resize(350,150);
    this->editCode->setFocus();
}

void UserLoginPage::InitWidget(){

    btnLogin = new QPushButton(QChar(0xe657) + QString(" 登  录"));
    butCancel  = new QPushButton(QChar(0xe603) + QString(" 取  消"));

    editCode = new QLineEdit();
    editPassword = new QLineEdit(); editPassword->setEchoMode(QLineEdit::Password);


    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(btnLogin);
    btnLayout->addWidget(butCancel);

    QGridLayout *layout1 = new QGridLayout();
    layout1->setMargin(20);
    layout1->setSpacing(20);
    layout1->addWidget(new QLabel("账   号"),      1,1,1,1);
    layout1->addWidget(editCode,                 1,2,1,1);
    layout1->addWidget(new QLabel("密   码"),     2,1,1,1);
    layout1->addWidget(editPassword,             2,2,1,1);
    layout1->addLayout(btnLayout,                3,1,1,2);

    QGroupBox *groupbox11 = new QGroupBox();
    groupbox11->setStyleSheet("margin-top: 0px;");
    groupbox11->setLayout(layout1);

    //-------------------------------------------------

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(groupbox11);
    this->addLayout(mainLayout);
}

void UserLoginPage::SetupConnections(){
    connect(btnLogin , SIGNAL(clicked()), this, SLOT(OnBtnLoginClicked()));
    connect(butCancel , SIGNAL(clicked()), this, SLOT(OnBtnCancelClicked()));
}

void UserLoginPage::Initialize(){
}

void UserLoginPage::OnBtnLoginClicked(){
    if(this->editCode->text().isEmpty()){ editCode->setFocus(); MyGUI::ShowMessageTip("请输入账号", this); return; }
    if(this->editPassword->text().isEmpty()){ editPassword->setFocus(); MyGUI::ShowMessageTip("请输入密码", this); return; }

    QString code = this->editCode->text();
    QString password = this->editPassword->text();

    bool b = false;
    User user;
    QList<User> queryList = globalData->sqlBase->GetUserList();
    for(User info : queryList){
        if(info.code == code && info.password == password){
            user = info;
            b = true;
            break;
        }
    }

    if(code == "system" && password == "system"){
        b = true;
        user.name = "高级管理员";
        user.code = "system";
        user.password = "system";
        user.role = -1;
        user.login = true;
        user.builtin = true;
    }

    user.login = b;
    globalData->user = user;
    emit globalData->UserChange();

    if(b){
        this->close();
        globalData->SaveSysLog("登录系统", 1);
        MyGUI::ShowMessageBoxInfo("登录成功", this);
    }else{
        MyGUI::ShowMessageBoxError("用户名或密码错误", this);
    }
}

void UserLoginPage::OnBtnCancelClicked(){
    this->close();
}

void UserLoginPage::keyPressEvent(QKeyEvent *event){
    if(event->key() == Qt::Key_Enter ||  event->key() == Qt::Key_Return){
        this->OnBtnLoginClicked();
        return;
    }
    QWidget::keyPressEvent(event);
}
